<?php

namespace app\qsapi\controller;

use app\qsapi\model\BoxGameCate;
use app\qsapi\model\GmApplyGame;
use app\qsapi\model\Member;
use think\cache\driver\Redis;

class Internal extends BaseController
{
    protected $noNeedLogin = [
        'checkToken', 'checktoken', 'createToken', 'createtoken', 'gameApply',
        'gameapply', 'updateGames', 'updategames', 'offShelf', 'offshelf', 'grounding',
        'gameCancel','gamecancel','getGameCates','getgamecates'
    ];


    public function createToken()
    {
        $token = (new \JwtGm())->getToken(9999);
        return json(['token' => $token]);
    }


    public function updateGames()
    {
        $params = $this->request->post();
        $data = GmApplyGame::updateGames($params, 1);
        return json($data);
    }


    public function offShelf()
    {
        $params = $this->request->post();
        $data = GmApplyGame::updateGames($params, 2);
        return json($data);
    }


    public function grounding()
    {
        $params = $this->request->post();
        $data = GmApplyGame::updateGames($params, 3);
        return json($data);
    }


    public function gameCancel()
    {
        $params = $this->request->post();
        $data = GmApplyGame::updateGames($params, 4);
        return json($data);
    }

    public function getGameCates()
    {
        $gameCates = (new BoxGameCate())->where(['mark' => 1,'status'=>1])->field('id,name')->select();
        $data = ['status' =>200,'msg'=>"获取成功",'data'=>$gameCates];
        return json($data);
    }


    //gm游戏申请接口
    public function gameApply()
    {
        $params = $this->request->post();
        $data = GmApplyGame::applyGame($params);
        return json($data);
    }


    /**
     * 内部校验 用户token
     * @return void
     */
    public function checkToken()
    {
        $sign = $this->request->post('token_str');
        $sign = decrypt($sign);
        $sign_arr = json_decode($sign, true);
        if (!isset($sign_arr['timestamp']) || !isset($sign_arr['token'])) {
            return json(api_message([], '参数错误', 0));
        }
        if (empty($sign_arr['timestamp']) || empty($sign_arr['token'])) {
            return json(api_message([], '参数错误', 0));
        }
        //5分钟内有效
        if (time() - $sign_arr['timestamp'] >= 3000) {
            return json(api_message([], '已过有效期', 0));
        }
        //校验redis
        $redis = new Redis([
            'host' => Env('redis.host'),
            'port' => Env('redis.port'),
            'password' => Env('redis.password'),
            'prefix = cqyqbox_'
        ]);
        $bool = $redis->has($sign_arr['token']);
        if (!$bool) {
            return json(api_message([], "token不存在", 401));
        }
        $jwt = new \Jwt();
        $userId = $jwt->verifyToken($sign_arr['token']);
        if (!$userId) {
            return json(api_message([], '用户错误', 0));
        }
        $info = (new Member())->field('id,username,nickname,avatar,package_id,game_id,pid')->where('id', $userId)->where('status', 1)->find();
        if (!$info) {
            return json(api_message([], '账号信息错误', 0));

        }
        return json(api_message([
            'main_user_id' => $info->pid == 0 ? $info->id : $info->pid,
            'final_user_id' => $info->id
        ]));

    }
}